package com.zhenyi.api.file;

import java.io.File;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import cn.hutool.core.date.DateUtil;

public class UploadFile {

	private String host = "http://res.etcr.com.cn/";
	// String domain = "http://res.etcr.com.cn.qiniudns.com/";
	private String ACCESS_KEY = "F_HmPwWkomtWFPZU7Ngv-3E3lygV7upnCDcLEnCe";
	private String SECRET_KEY = "TIwevT4iOu_OYGtX2azq36g8mQQ2FH0ggG23UjrX";
	private String bucketname = "yitai";

	public String uploadQN(File file) throws QiniuException {
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		String token = auth.uploadToken(bucketname);
		UploadManager uploadManager = new UploadManager();
		String path = file.getAbsolutePath();
		String key = System.currentTimeMillis() + "";
		String url = "/" + DateUtil.today() + "/" + key;
		Response res = uploadManager.put(path, bucketname + url, token);
		if (!res.isOK()) {
			return "";
		}
		// System.out.println("bodyString: " + res.bodyString());
		String imageUrl = host + bucketname + url;
		return imageUrl;
	}
}
